package com.inei.reportedeverificacion.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.http.protocol.HTTP;
import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.os.Environment;
import android.util.Xml;

import com.inei.reportedeverificacion.controller.HogarController;
import com.inei.reportedeverificacion.controller.ReporteCap11Controller;
import com.inei.reportedeverificacion.controller.ReporteCap12Controller;
import com.inei.reportedeverificacion.controller.ReporteCap2Controller;
import com.inei.reportedeverificacion.controller.ReporteController;
import com.inei.reportedeverificacion.model.entity.Hogar;
import com.inei.reportedeverificacion.model.entity.Reporte;
import com.inei.reportedeverificacion.model.entity.ReporteCap11;
import com.inei.reportedeverificacion.model.entity.ReporteCap12;
import com.inei.reportedeverificacion.model.entity.ReporteCap2;

public class Exportar {

	Context context;

	public Exportar(Context context) {
		this.context = context;
	}

	public boolean xml(String CLAVEVIV) {
		File file = null;
		File ruta = null;
		try {
			File root = Environment.getExternalStorageDirectory();
			ruta = new File(root, "/Reporte de Verificacion/");
			if (!ruta.exists()) {
				ruta.mkdirs();
			}
			file = new File(ruta, "Reporte de Verificacion" + CLAVEVIV + ".xml");
			FileOutputStream fos = new FileOutputStream(file);
			XmlSerializer xml = Xml.newSerializer();
			xml.setOutput(fos, HTTP.UTF_8);
			xml.startDocument(HTTP.UTF_8, true);

			xml.startTag("", "script");

			HogarController hCon = new HogarController(context);
			List<Hogar> lHogar = hCon.HogarLeerViv(CLAVEVIV);
			for (Hogar h : lHogar) {

				ReporteController con = new ReporteController(context);
				Reporte r = new Reporte();
				r.setCLAVEVIVHOGAR(h.getCLAVEVIVHOGAR());
				r = con.ReporteBuscar(r);
				if (r != null) {
					xml.startTag("", "Reporte");

					xml.startTag("", "CLAVEVIVHOGAR");
					xml.text(obtenerValor(r.getCLAVEVIVHOGAR()));
					xml.endTag("", "CLAVEVIVHOGAR");

					xml.startTag("", "CLAVEVIV");
					xml.text(CLAVEVIV);
					xml.endTag("", "CLAVEVIV");

					xml.startTag("", "HOGAR");
					xml.text(obtenerValor(r.getHOGAR()));
					xml.endTag("", "HOGAR");

					xml.startTag("", "VIVREM");
					xml.text(obtenerValor(r.getVIVREM()));
					xml.endTag("", "VIVREM");

					xml.startTag("", "C1P15_RFINAL");
					xml.text(obtenerValor(r.getC1P15_RFINAL()));
					xml.endTag("", "C1P15_RFINAL");

					xml.startTag("", "C1P18");
					xml.text(obtenerValor(r.getC1P18()));
					xml.endTag("", "C1P18");

					xml.startTag("", "T_05_DIG_CAP03_COUNT");
					xml.text(obtenerValor(r.getT_05_DIG_CAP03_COUNT()));
					xml.endTag("", "T_05_DIG_CAP03_COUNT");

					xml.startTag("", "C1P59");
					xml.text(obtenerValor(r.getC1P59()));
					xml.endTag("", "C1P59");

					xml.startTag("", "C1P67_1");
					xml.text(obtenerValor(r.getC1P67_1()));
					xml.endTag("", "C1P67_1");

					xml.startTag("", "C1P67_2");
					xml.text(obtenerValor(r.getC1P67_2()));
					xml.endTag("", "C1P67_2");

					xml.startTag("", "C1P67_3");
					xml.text(obtenerValor(r.getC1P67_3()));
					xml.endTag("", "C1P67_3");

					xml.startTag("", "C1P67_4");
					xml.text(obtenerValor(r.getC1P67_4()));
					xml.endTag("", "C1P67_4");

					xml.startTag("", "C1P67_5");
					xml.text(obtenerValor(r.getC1P67_5()));
					xml.endTag("", "C1P67_5");

					xml.startTag("", "C1P67_6");
					xml.text(obtenerValor(r.getC1P67_6()));
					xml.endTag("", "C1P67_6");

					xml.startTag("", "C1P67_7");
					xml.text(obtenerValor(r.getC1P67_7()));
					xml.endTag("", "C1P67_7");

					xml.startTag("", "C1P67_8");
					xml.text(obtenerValor(r.getC1P67_8()));
					xml.endTag("", "C1P67_8");

					xml.startTag("", "C1P67_9");
					xml.text(obtenerValor(r.getC1P67_9()));
					xml.endTag("", "C1P67_9");

					xml.startTag("", "C1P67_10");
					xml.text(obtenerValor(r.getC1P67_10()));
					xml.endTag("", "C1P67_10");

					xml.startTag("", "C1P67_11");
					xml.text(obtenerValor(r.getC1P67_11()));
					xml.endTag("", "C1P67_11");

					xml.startTag("", "C1P69");
					xml.text(obtenerValor(r.getC1P69()));
					xml.endTag("", "C1P69");

					xml.startTag("", "C1P70");
					xml.text(obtenerValor(r.getC1P70()));
					xml.endTag("", "C1P70");

					xml.startTag("", "C1P78_1");
					xml.text(obtenerValor(r.getC1P78_1()));
					xml.endTag("", "C1P78_1");

					xml.startTag("", "C1P79_1");
					xml.text(obtenerValor(r.getC1P79_1()));
					xml.endTag("", "C1P79_1");

					xml.startTag("", "C1P78_2");
					xml.text(obtenerValor(r.getC1P78_2()));
					xml.endTag("", "C1P78_2");

					xml.startTag("", "C1P79_2");
					xml.text(obtenerValor(r.getC1P79_2()));
					xml.endTag("", "C1P79_2");

					xml.startTag("", "C1P78_3");
					xml.text(obtenerValor(r.getC1P78_3()));
					xml.endTag("", "C1P78_3");

					xml.startTag("", "C1P79_3");
					xml.text(obtenerValor(r.getC1P79_3()));
					xml.endTag("", "C1P79_3");

					xml.startTag("", "C1P78_4");
					xml.text(obtenerValor(r.getC1P78_4()));
					xml.endTag("", "C1P78_4");

					xml.startTag("", "C1P79_4");
					xml.text(obtenerValor(r.getC1P79_4()));
					xml.endTag("", "C1P79_4");

					xml.startTag("", "C1P78_5");
					xml.text(obtenerValor(r.getC1P78_5()));
					xml.endTag("", "C1P78_5");

					xml.startTag("", "C1P79_5");
					xml.text(obtenerValor(r.getC1P79_5()));
					xml.endTag("", "C1P79_5");

					xml.startTag("", "C1P78_6");
					xml.text(obtenerValor(r.getC1P78_6()));
					xml.endTag("", "C1P78_6");

					xml.startTag("", "C1P79_6");
					xml.text(obtenerValor(r.getC1P79_6()));
					xml.endTag("", "C1P79_6");

					xml.startTag("", "C1P78_7");
					xml.text(obtenerValor(r.getC1P78_7()));
					xml.endTag("", "C1P78_7");

					xml.startTag("", "C1P79_7");
					xml.text(obtenerValor(r.getC1P79_7()));
					xml.endTag("", "C1P79_7");

					xml.startTag("", "C1P78_8");
					xml.text(obtenerValor(r.getC1P78_8()));
					xml.endTag("", "C1P78_8");

					xml.startTag("", "C1P79_8");
					xml.text(obtenerValor(r.getC1P79_8()));
					xml.endTag("", "C1P79_8");

					xml.startTag("", "C1P78_9");
					xml.text(obtenerValor(r.getC1P78_9()));
					xml.endTag("", "C1P78_9");

					xml.startTag("", "C1P79_9");
					xml.text(obtenerValor(r.getC1P79_9()));
					xml.endTag("", "C1P79_9");

					xml.startTag("", "C1P78_10");
					xml.text(obtenerValor(r.getC1P78_10()));
					xml.endTag("", "C1P78_10");

					xml.startTag("", "C1P79_10");
					xml.text(obtenerValor(r.getC1P79_10()));
					xml.endTag("", "C1P79_10");

					xml.startTag("", "C1P78_11");
					xml.text(obtenerValor(r.getC1P78_11()));
					xml.endTag("", "C1P78_11");

					xml.startTag("", "C1P79_11");
					xml.text(obtenerValor(r.getC1P79_11()));
					xml.endTag("", "C1P79_11");

					xml.startTag("", "C1P84");
					xml.text(obtenerValor(r.getC1P84()));
					xml.endTag("", "C1P84");

					xml.startTag("", "C1P85");
					xml.text(obtenerValor(r.getC1P85()));
					xml.endTag("", "C1P85");

					xml.startTag("", "C1P95");
					xml.text(obtenerValor(r.getC1P95()));
					xml.endTag("", "C1P95");

					xml.startTag("", "C1P97");
					xml.text(obtenerValor(r.getC1P97()));
					xml.endTag("", "C1P97");

					xml.startTag("", "C1P97_COUNT");
					xml.text(obtenerValor(r.getC1P97_COUNT()));
					xml.endTag("", "C1P97_COUNT");

					xml.startTag("", "C1P109");
					xml.text(obtenerValor(r.getC1P109()));
					xml.endTag("", "C1P109");

					xml.startTag("", "C1P118");
					xml.text(obtenerValor(r.getC1P118()));
					xml.endTag("", "C1P118");

					xml.startTag("", "C1P132");
					xml.text(obtenerValor(r.getC1P132()));
					xml.endTag("", "C1P132");

					xml.startTag("", "C1P142");
					xml.text(obtenerValor(r.getC1P142()));
					xml.endTag("", "C1P142");

					xml.startTag("", "C1P142_COUNT");
					xml.text(obtenerValor(r.getC1P142_COUNT()));
					xml.endTag("", "C1P142_COUNT");

					xml.startTag("", "getC1P340_INF");
					xml.text(obtenerValor(r.getC1P340_INF()));
					xml.endTag("", "getC1P340_INF");

					xml.endTag("", "Reporte");
				}else{
					throw new Exception();
				}

				ReporteCap2Controller r2Con = new ReporteCap2Controller(context);
				List<ReporteCap2> lReporte2 = r2Con.ReporteCap2LeerXHogar(h
						.getCLAVEVIVHOGAR());
				if (lReporte2 != null) {
					for (ReporteCap2 r2 : lReporte2) {
						xml.startTag("", "ReporteCap2");

						xml.startTag("", "CLAVEVIVHOGAR");
						xml.text(obtenerValor(r2.getCLAVEVIVHOGAR()));
						xml.endTag("", "CLAVEVIVHOGAR");

						xml.startTag("", "NRO_ORDEN");
						xml.text(obtenerValor(r2.getNRO_ORDEN()));
						xml.endTag("", "NRO_ORDEN");

						xml.startTag("", "codigo");
						xml.text(obtenerValor(r2.getCodigo()));;
						xml.endTag("", "codigo");

						xml.endTag("", "ReporteCap2");
					}
				}
				ReporteCap11Controller r11Con = new ReporteCap11Controller(
						context);
				List<ReporteCap11> lReporte11 = r11Con.ReporteCap11LeerXHOGAR(h
						.getCLAVEVIVHOGAR());
				if (lReporte11 != null) {
					for (ReporteCap11 r11 : lReporte11) {
						xml.startTag("", "ReporteCap11");

						xml.startTag("", "CLAVEVIVHOGAR");
						xml.text(obtenerValor(r11.getCLAVEVIVHOGAR()));
						xml.endTag("", "CLAVEVIVHOGAR");

						xml.startTag("", "NRO_ORDEN");
						xml.text(obtenerValor(r11.getNRO_ORDEN()));
						xml.endTag("", "NRO_ORDEN");

						xml.startTag("", "C1P158");
						xml.text(obtenerValor(r11.getC1P158()));
						xml.endTag("", "C1P158");

						xml.startTag("", "C1P173");
						xml.text(obtenerValor(r11.getC1P173()));
						xml.endTag("", "C1P173");

						xml.endTag("", "ReporteCap11");
					}
				}

				ReporteCap12Controller r12Con = new ReporteCap12Controller(
						context);
				List<ReporteCap12> lReporte12 = r12Con.ReporteCap12LeerXHOGAR(h
						.getCLAVEVIVHOGAR());
				if (lReporte12 != null) {
					for (ReporteCap12 r12 : lReporte12) {
						xml.startTag("", "ReporteCap12");

						xml.startTag("", "CLAVEVIVHOGAR");
						xml.text(obtenerValor(r12.getCLAVEVIVHOGAR()));
						xml.endTag("", "CLAVEVIVHOGAR");

						xml.startTag("", "NRO_ORDEN");
						xml.text(obtenerValor(r12.getNRO_ORDEN()));
						xml.endTag("", "NRO_ORDEN");

						xml.startTag("", "T_05_DIG_CAP12_2");
						xml.text(obtenerValor(r12.getT_05_DIG_CAP12_2()));
						xml.endTag("", "T_05_DIG_CAP12_2");

						xml.startTag("", "T_05_DIG_CAP12_4");
						xml.text(obtenerValor(r12.getT_05_DIG_CAP12_4()));
						xml.endTag("", "T_05_DIG_CAP12_4");

						xml.startTag("", "T_05_DIG_CAP12_5");
						xml.text(obtenerValor(r12.getT_05_DIG_CAP12_5()));
						xml.endTag("", "T_05_DIG_CAP12_5");

						xml.startTag("", "T_05_DIG_CAP12_7");
						xml.text(obtenerValor(r12.getT_05_DIG_CAP12_7()));
						xml.endTag("", "T_05_DIG_CAP12_7");

						xml.startTag("", "T_05_DIG_CAP12_8");
						xml.text(obtenerValor(r12.getT_05_DIG_CAP12_8()));
						xml.endTag("", "T_05_DIG_CAP12_8");

						xml.startTag("", "T_05_DIG_CAP12_9");
						xml.text(obtenerValor(r12.getT_05_DIG_CAP12_9()));
						xml.endTag("", "T_05_DIG_CAP12_9");

						xml.endTag("", "ReporteCap12");
					}
				}
			}

			xml.endTag("", "script");
			xml.endDocument();
			fos.close();

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public String obtenerValor(int txt) {
		return txt + "";

	}

	public String obtenerValor(String txt) {
		if (txt == null) {
			return "";
		} else {
			return txt;
		}

	}

}
